#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QGraphicsDropShadowEffect>
#include <QTextStream>

#include <Dlg/softinfodlg.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initializePage();


    m_pMain = new MainForm();
    m_pSetPlans = new SetPlansForm();
    m_pVersions = new VersionsForm();
    m_pSystem = new SystemForm();
    m_pData = new DataForm();
    m_pAlgTest = new AlgTestForm();

    ui->gridLayout->addWidget(m_pMain,0,1);
    ui->gridLayout->addWidget(m_pSetPlans,0,1);
    ui->gridLayout->addWidget(m_pVersions,0,1);
    ui->gridLayout->addWidget(m_pSystem,0,1);
    ui->gridLayout->addWidget(m_pData,0,1);
    ui->gridLayout->addWidget(m_pAlgTest,0,1);

    m_pMain->hide();
    m_pSetPlans->hide();
    m_pVersions->hide();
    m_pSystem->hide();
    m_pData->hide();
    m_pAlgTest->hide();


    qFrom = m_pMain;
    SwitchFrom(0);

    connect(m_pAlgTest, &AlgTestForm::sigAlgTestPath,m_pMain, &MainForm::slotAlgTestPath);
    connect(m_pMain, &MainForm::sigAlgTestResult,m_pAlgTest, &AlgTestForm::handleAlgorithmResult);
    connect(m_pMain, &MainForm::sigToMotor,m_pSystem->m_pMotor, &MotorForm::soltInMain);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initializePage()
{
    QFile file(":/qss/main.qss");
    if (file.open(QFile::ReadOnly))
    {
        QTextStream filetext(&file);
        QString stylesheet = filetext.readAll();
        this->setStyleSheet(stylesheet);
        file.close();
    }

    //内层窗口添加对应的阴影效果
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);
    shadow_effect->setColor(QColor(150,150,150));
    shadow_effect->setBlurRadius(6);
    ui->widget_nav->setGraphicsEffect(shadow_effect);
}

void MainWindow::SwitchFrom(int index)
{
    QString info;
    qFrom->hide();
    switch (index) {
    case 0:
        info = tr("main");
        qFrom = m_pMain;
        break;
    case 1:
        info = tr("set plans");
        qFrom = m_pSetPlans;
        break;
    case 2:
        info = tr("debug alg");
        qFrom = m_pAlgTest;
        break;
    case 3:
        info = tr("data infomation");
        qFrom = m_pData;
        break;
    case 4:
        info = tr("set system");
        qFrom = m_pSystem;
        break;
    case 5:
        info = tr("versions");
        qFrom = m_pVersions;
        break;
    case 6:
        info = tr("soft info");
        break;
    }
    ui->navlabel->setText(info);
    qFrom->show();
}

void MainWindow::appQuit()
{
    m_pMain->stopAll();
}

void MainWindow::on_Bt_main_clicked()
{
    SwitchFrom(0);
}


void MainWindow::on_Bt_plan_clicked()
{
    SwitchFrom(1);
}


void MainWindow::on_Bt_debug_clicked()
{
    SwitchFrom(2);
}


void MainWindow::on_Bt_information_clicked()
{
    SwitchFrom(3);
}


void MainWindow::on_Bt_system_clicked()
{
    SwitchFrom(4);
}


void MainWindow::on_Bt_versions_clicked()
{
    SwitchFrom(5);
}


void MainWindow::on_Bt_info_clicked()
{
    SwitchFrom(6);
    SoftInfoDlg* m_pSoft = new SoftInfoDlg();
    m_pSoft->show();
}

